package com.ygqh.baby.controller.admin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygqh.baby.annotation.ProductLog;
import com.ygqh.baby.ao.CheckStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.YgProductComment;
import com.ygqh.baby.service.YgProductCommentService;
import com.ygqh.baby.utils.JsonUtils;

@Controller
@RequestMapping("/admin/productComment")
public class ProductCommentController extends BaseController<Object, java.lang.Long> {
	@Autowired
	private YgProductCommentService ygProductCommentService;

	@RequestMapping("searchProductComment")
	@ResponseBody
	public Map<String, Object> searchProductComment(String q, QueryInfo queryInfo, Date startDate, Date endDate, CheckStatus pStatus, Boolean isUserComment,String userName,Long orderId)
			throws ParseException {

		ResultSet<YgProductComment> resultSet = ygProductCommentService.searchForHouTai(queryInfo, null, q, pStatus, startDate, endDate, isUserComment,userName,orderId);

		return resultSet.toDatatables(null);
	}

	// 审批
	@RequestMapping("approveProductComment")
	@ResponseBody
	public Message approveProductComment(Long id, String remark, CheckStatus status) {
		YgProductComment ygProductComment = new YgProductComment();
		ygProductComment.setId(id);
		ygProductComment.setCheckRemark(remark);
		ygProductComment.setCheckStatus(status);
		ygProductComment.setCheckBy(getSession().getTrueName());
		ygProductComment.setCheckTime(new Date());
		ygProductCommentService.update(ygProductComment);
		return SUCCESS_MESSAGE;
	}

	// 审批
	@RequestMapping("replySave")
	@ResponseBody
	public Message replySave(Long id, String replyContent) {
		YgProductComment ygProductComment = new YgProductComment();
		ygProductComment.setId(id);
		ygProductComment.setReplyContent(replyContent);
		ygProductComment.setReplyBy(getSession().getTrueName());
		ygProductComment.setReplyTime(new Date());
		ygProductCommentService.update(ygProductComment);
		return SUCCESS_MESSAGE;
	}

	/**
	 * 批量审批
	 * 
	 * @param id
	 * @param remark
	 * @param status
	 * @return
	 */
	@RequestMapping("batchApproveProductComment")
	@ResponseBody
	public Message batchApproveProductComment(Long[] ids, String remark, String status) {
		ygProductCommentService.updateBatchChechStatus(getSession().getTrueName(), status, ids);
		return SUCCESS_MESSAGE;
	}

	@ProductLog(description = "添加商品评价", params = {"productComment", "createTime"})
	@RequestMapping("addProductComment")
	@ResponseBody
	public Message addProductComment(String productComment, String createTime) throws ParseException {
		YgProductComment p = JsonUtils.toObject(productComment, YgProductComment.class);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		p.setCreateTime(df.parse(createTime));
		ygProductCommentService.save(p);
		return Message.success(p.getProductId());
	}

}